package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.LeaseDetailBean;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.WarnPop;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class LeaseDVM extends TitleVM {

    public ObservableField<LeaseDetailBean.DataDTO> entity = new ObservableField<>();
    public ObservableField<Integer> lID = new ObservableField<>();

    public LeaseDVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("租约详情");
        setRightIconVisible(View.VISIBLE);
        setRightImg(R.mipmap.title_more);
    }



    public ObservableField<String> rent = new ObservableField<>();

    @Override
    protected void rightIconOnClick() {
        super.rightIconOnClick();
        WarnPop warnPop = new WarnPop(mActivity, "删除", "确认删除？删除之后数据无法恢复。", new WarnPop.OnSelCallBack() {
            @Override
            public void onCallBack(String s) {
                delLease();
            }
        });
        PopUtils.showPop(mActivity,warnPop,true);
    }

    private void delLease() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("sid",lID.get()+"");
        model.deletelease(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult o) {
                        if (o.issucc){
                            ToastUtils.showShort(o.msg);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getDetailData();
    }

    private void getDetailData() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("lid",lID.get()+"");
        model.lease_detail(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<LeaseDetailBean>() {
                    @Override
                    public void onNext(LeaseDetailBean o) {
                        if (o.issucc){
                            entity.set(o.getData());
                            if (entity.get().getCategory().equals("合租")){
                                if (entity.get().getRoomDetails().size() > 0){
                                    rent.set(entity.get().getRoomDetails().get(0).getRent());
                                }else {
                                    rent.set("未填写");
                                }

                            }else {
                                rent.set(entity.get().getRent());
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
